package com.text2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Gai {
    public static void main(String[] args) throws SQLException {
        Gai gai = new Gai();
        int count=gai.updateById("10",1);


        if(count>0){
            System.out.println("修改成功");
        }
    }


    public int updateById(String ip,int id) throws  SQLException{
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        int rows=0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String username="root";
            String password="123456";
            connection=DriverManager.getConnection(url,username,password);
            String sql="UPDATE news_comment SET ip=?WHERE id=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,ip);

            preparedStatement.setInt(2,id);
            rows=preparedStatement.executeUpdate();



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            preparedStatement.close();
            connection.close();
        }
        return rows;
    }

}
